package com.ruoyi.system.service.impl;

import java.util.Collections;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysHolidayMapper;
import com.ruoyi.system.domain.SysHoliday;
import com.ruoyi.system.service.ISysHolidayService;
import com.ruoyi.common.core.text.Convert;

/**
 * 节假日Service业务层处理
 * 
 * @author maoxiaodong
 * @date 2025-04-24
 */
@Service
public class SysHolidayServiceImpl implements ISysHolidayService 
{
    @Autowired
    private SysHolidayMapper sysHolidayMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询节假日
     * 
     * @param id 节假日主键
     * @return 节假日
     */
    @Override
    public SysHoliday selectSysHolidayById(Long id)
    {
        return sysHolidayMapper.selectSysHolidayById(id);
    }

    /**
     * 查询节假日列表
     * 
     * @param sysHoliday 节假日
     * @return 节假日
     */
    @Override
    @DataScope(deptAlias = "h")
    public List<SysHoliday> selectSysHolidayList(SysHoliday sysHoliday)
    {
        return sysHolidayMapper.selectSysHolidayList(sysHoliday);
    }

    /**
     * 新增节假日
     * 
     * @param sysHoliday 节假日
     * @return 结果
     */
    @Override
    public int insertSysHoliday(SysHoliday sysHoliday)
    {
        Long deptId = ShiroUtils.getSysUser().getDeptId();
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        if(sysDept != null){
            String [] ancestors =  sysDept.getAncestors().split(",");
            if(ancestors.length > 1){
                sysHoliday.setDeptId(Long.valueOf(ancestors[1]));
            }else{
                sysHoliday.setDeptId(deptId);
            }

        }
        sysHoliday.setCreateTime(DateUtils.getNowDate());
        return sysHolidayMapper.insertSysHoliday(sysHoliday);
    }

    /**
     * 修改节假日
     * 
     * @param sysHoliday 节假日
     * @return 结果
     */
    @Override
    public int updateSysHoliday(SysHoliday sysHoliday)
    {
        Long deptId = ShiroUtils.getSysUser().getDeptId();
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        if(sysDept != null){
            String [] ancestors =  sysDept.getAncestors().split(",");
            if(ancestors.length > 1){
                sysHoliday.setDeptId(Long.valueOf(ancestors[1]));
            }else{
                sysHoliday.setDeptId(deptId);
            }

        }
        sysHoliday.setUpdateTime(DateUtils.getNowDate());
        return sysHolidayMapper.updateSysHoliday(sysHoliday);
    }

    /**
     * 批量删除节假日
     * 
     * @param ids 需要删除的节假日主键
     * @return 结果
     */
    @Override
    public int deleteSysHolidayByIds(String ids)
    {
        return sysHolidayMapper.deleteSysHolidayByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除节假日信息
     * 
     * @param id 节假日主键
     * @return 结果
     */
    @Override
    public int deleteSysHolidayById(Long id)
    {
        return sysHolidayMapper.deleteSysHolidayById(id);
    }

    @Override
    public List<SysHoliday> selectSysHolidayListByIds(List<Long> ids) {
        return sysHolidayMapper.selectSysHolidayListByIds(ids);
    }

    @Override
    public List<SysHoliday> selectSysHolidayListByYear(Long deptId, String year) {
        return sysHolidayMapper.selectSysHolidayListByYear(deptId, year);
    }
}
